--[[
Author: wangxiaoyan
LastEditTime: 2021-01-21 16:16:03
FilePath: \Lua\View\UIBase\UIComponent\UIComponentScrollRectEx.lua

拥有近景、远景功能滑动ScrollRect

当主图在移动时，另外的图片需要以一定的速度去配合主图移动
--]]

require 'View/UIBase/UIComponent/UIComponentBase'

UIComponentScrollRectEx = Class("UIComponentScrollRectEx",UIComponentBase)
local _M = UIComponentScrollRectEx

_M.STOP_DELTA = 0.001

function _M:OnCreate()
    self.mainLayer = nil            -- 主层 Viewport/Content
    self.otherLayers = nil          -- 远近景层
    self.mainLayerDefaultPos = nil

    self.cacheMoveDiff = nil
    self.cacheMainPrePos = nil
    self:RegisterUpdate()
end

function _M:OnEnable()
    self.mainLayerDefaultPos = self.mainLayer.position
    self.cacheMoveDiff = Vector3(0,0,0)
    self.cacheMainPrePos = self.mainLayer.position

    for key, layer in pairs(self.otherLayers) do
        layer:OnEnable()
    end
end

function _M:OnDisable()
    for key, layer in pairs(self.otherLayers) do
        layer:OnDisable()
    end
end

function _M:OnDestroy()
    for key, layer in pairs(self.otherLayers) do
        layer:OnDestroy()
    end

    self.mainLayer = nil
    self.mainLayerDefaultPos = nil
    self.otherLayers = nil
    self.cachePreMoveDiff = nil
end

function _M:OnUpdate()
    local x , y , z = Util.Lua_GetPositionXYZ(self.mainLayer,0,0,0)
    self.cacheMoveDiff.x = x - self.cacheMainPrePos.x
    self.cacheMoveDiff.y = y - self.cacheMainPrePos.y
    self.cacheMoveDiff.z = z - self.cacheMainPrePos.z
    if not self:CheckDiff()then 
        return
    end 
    self.cacheMainPrePos.x = x
    self.cacheMainPrePos.y = y
    self.cacheMainPrePos.z = z
    for key, layer in pairs(self.otherLayers) do
        layer:Move(self.cacheMoveDiff)
    end
end

--- 一键初始化所有层
--- mainLayer 主层 其他层跟随移动
function _M:InitLayerFGUI(mainLayer,...)
    local layers = {}
    for key, value in pairs({...}) do
        table.insert(layers,{gameObject=value.displayObject.gameObject,data=value.data})
    end
    self:InitLayer(mainLayer.displayObject.gameObject,unpack(layers))
end
function _M:InitLayer(mainLayer,...)
    self:SetMainLayer(mainLayer)
    self.otherLayers = {}
    for key, value in pairs({...}) do
        if value ~= nil then 
            self:AddLayer(value.gameObject,tonumber(value.data))
        end
    end
end
function _M:InitLayerOhters(mainLayer,otherLayerParent,isNegated)
    if otherLayerParent == nil then return end
    self:SetMainLayer(mainLayer)
    self.otherLayers = {}
    local childCount = otherLayerParent.childCount
    for i = 0, childCount-1 do
        local layer = otherLayerParent:GetChild(i)
        local params = string_split(layer.name,"_")
        if #params == 2 then 
            local relativeSpeed = tonumber(params[2])
            if isNegated then 
                relativeSpeed = -relativeSpeed
            end
            self:AddLayer(layer,relativeSpeed)
        end
    end
end

function _M:SetMainLayer(mainLayer)
    self.mainLayer = mainLayer.transform
end

--- 增加一个层
function _M:AddLayer(layerContent,relativeSpeed)
    local tempLayer = UIComponentScrollRectEx_Layer.New()
    tempLayer.layer = layerContent.transform
    tempLayer.relativeSpeed = relativeSpeed or 0
    table.insert(self.otherLayers,tempLayer)
end

function _M:CheckDiff()
    return math.abs(self.cacheMoveDiff.x) > self.STOP_DELTA 
        or math.abs(self.cacheMoveDiff.y) > self.STOP_DELTA 
        or math.abs(self.cacheMoveDiff.z) > self.STOP_DELTA
end

--- 层（远景、近景）
UIComponentScrollRectEx_Layer = Class("UIComponentScrollRectEx_Layer")
local _M_L = UIComponentScrollRectEx_Layer

function _M_L:ctor()
    self.layer = nil            -- 层（远景、近景）对象
    self.relativeSpeed = 1      -- 相对于滑动的相对速度 1 表示和主层（UGUI中ScrllRect下Content）滑动速度一样
end

function _M_L:OnEnable()
end

function _M_L:OnDisable()
    
end

function _M_L:Move(moveDiff)
    local tmp = Vector3.MulEX(moveDiff,self.relativeSpeed);
    Util.Lua_TransformAddPosition(self.layer, tmp.x, tmp.y, tmp.z);
end

function _M_L:OnDestroy()
    self.layer = nil
end